/*  io-redirect.c - dgsh - shell like bash
 * Autor        Diego Gomes da Silva    nUSP 7656467
 *              Alfredo Lima Porfirio   nUSP 7236614
 * Sistemas Operacionais 2
 * Professor Monaco
 * ICMC/USP
 */



#include <io-redirect.h>
#include <dgsh.h>




void open_io_redirect ( ControlDgsh * controlDgsh )
{
        if ( controlDgsh->size_args >= 3 && strcmp ( controlDgsh->command[0][controlDgsh->size_args-3], ">" ) == 0 ) {
        controlDgsh->fd_out = dup ( 1 );
        fflush ( stdout );
        close ( 1 );

        if ( ( controlDgsh->fd_out2   = open ( controlDgsh->command[0][controlDgsh->size_args-2], O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR ) ) < 0 ) fatal(controlDgsh->command[0][0]);

        controlDgsh->size_args -= 3;
        controlDgsh->command[0][controlDgsh->size_args] = NULL;
        controlDgsh->flag_out = 1;
        if ( controlDgsh->size_args >= 5 && strcmp ( controlDgsh->command[0][controlDgsh->size_args-5], "<" ) == 0 ) {
            controlDgsh->fd_in   = dup ( 1 );
            fflush ( stdin );
            close ( 1 );

            if ( ( controlDgsh->fd_in2  = open ( controlDgsh->command[0][controlDgsh->size_args-4],O_RDONLY ) ) < 0 ) fatal(controlDgsh->command[0][0]);

            controlDgsh->size_args -= 5;
            controlDgsh->command[0][controlDgsh->size_args] = NULL;
            controlDgsh->flag_in = 1;

        }
        } else         if ( controlDgsh->size_args >= 3 && strcmp ( controlDgsh->command[0][controlDgsh->size_args-3], ">>" ) == 0 ) {

        controlDgsh->fd_out  = dup ( 1 );
        fflush ( stdout );
        close ( 1 );

        if ( ( controlDgsh->fd_out2  = open ( controlDgsh->command[0][controlDgsh->size_args-2], O_CREAT | O_APPEND | O_RDWR, S_IRUSR | S_IWUSR ) ) < 0 ) fatal(controlDgsh->command[0][0]);

        controlDgsh->size_args -= 3;
        controlDgsh->command[0][controlDgsh->size_args] = NULL;
        controlDgsh->flag_out = 1;
        if ( controlDgsh->size_args >= 5 && strcmp ( controlDgsh->command[0][controlDgsh->size_args-5], "<" ) == 0 ) {
            controlDgsh->fd_in   = dup ( 1 );
            fflush ( stdin );
            close ( 1 );

            if ( ( controlDgsh->fd_in2  = open ( controlDgsh->command[0][controlDgsh->size_args-4],O_RDONLY ) ) < 0 ) fatal(controlDgsh->command[0][0]);

            controlDgsh->size_args -= 5;
            controlDgsh->command[0][controlDgsh->size_args] = NULL;
            controlDgsh->flag_in = 1;

        }
    } else if ( controlDgsh->size_args >= 3 && strcmp ( controlDgsh->command[0][controlDgsh->size_args-3], "<" ) == 0 ) {
        controlDgsh->fd_in   = dup ( 1 );
        fflush ( stdin );
        close ( 1 );

        if ( ( controlDgsh->fd_in2  = open ( controlDgsh->command[0][controlDgsh->size_args-4],O_RDONLY ) ) < 0 ) fatal(controlDgsh->command[0][0]);

        controlDgsh->size_args -= 5;
        controlDgsh->command[0][controlDgsh->size_args] = NULL;
        controlDgsh->flag_in = 1;

    }
}

void close_io_redirect ( ControlDgsh * controlDgsh )
{

    if ( controlDgsh->flag_out == 1 ) {
        fflush ( stdout );
        close ( controlDgsh->fd_out2 );
        dup ( controlDgsh->fd_out );
        close ( controlDgsh->fd_out );
        controlDgsh->flag_out = 0;
    }
    
    
    if ( controlDgsh->flag_in == 1 ) {
        fflush ( stdin );
        close ( controlDgsh->fd_in2 );
        dup ( controlDgsh->fd_in );
        close ( controlDgsh->fd_in );
        controlDgsh->flag_out = 0;
    }
}
